﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using QLibrary.Engine;
using QService.Service;
using QModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using QLibrary.Helper;

namespace QCats.ViewModel
{
    public class InformationVM : QCatsViewModel
    {
        #region Fields
        private HttpEngine _httpEngine;
        private ObservableCollection<FeedsEntity> _feedsItems;
        #endregion

        #region Constructor
        public InformationVM()
        {
            _feedsItems = new ObservableCollection<FeedsEntity>();
        }
        #endregion

        #region Properties
        public ObservableCollection<FeedsEntity> FeedsItems
        {
            get { return _feedsItems; }
            set
            {
                _feedsItems = value;
                this.RaisePropertyChanged(() => this.FeedsItems);
            }
        }
        #endregion

        #region EventHandler
        void _httpEngine_FetchDataCompleted(object sender, FetchCompletedEventArgs e)
        {
            if (e.ErrorType == ErrorType.Err_Null)
            {
                List<FeedsEntity> feedsList = e.ResultData["FeedsList"] as List<FeedsEntity>;
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        feedsList.ToObservableCollection<FeedsEntity>(FeedsItems);
                    });
            }
        }
        #endregion

        #region Override
        public override void OnNavigatedTo(NavigationEventArgs navigationEventArgs)
        {
            base.OnNavigatedTo(navigationEventArgs);
            _httpEngine = RRService.GetFeeds();
            _httpEngine.FetchDataCompleted += _httpEngine_FetchDataCompleted;
            _httpEngine.FetchData();
        }

        public override void OnNavigatedFrom(NavigationEventArgs navigationEventArgs)
        {
            base.OnNavigatedFrom(navigationEventArgs);
        }
        #endregion

    }
}
